export const DataUtils = {
    // 封装倒计时计算方法
    formatCountdown: function (targetTime) {
        // 处理ios 不兼容格式
        let timeStr = (typeof targetTime === 'string' ? targetTime : targetTime * (targetTime < 1e12 ? 1000 : 1)).replace(/-/g, '/')
        // 统一转换为时间戳（处理秒级/毫秒级时间戳及日期字符串）
        const target = new Date(timeStr).getTime();
        const now = Date.now();
        let diff = target - now;

        // 处理过期情况
        if (diff <= 0) return '已过期';

        // 计算各时间单位
        const days = Math.floor(diff / (1000 * 60 * 60 * 24));
        diff %= 1000 * 60 * 60 * 24;
        const hours = Math.floor(diff / (1000 * 60 * 60));
        diff %= 1000 * 60 * 60;
        const minutes = Math.floor(diff / (1000 * 60));
        diff %= 1000 * 60;
        const seconds = Math.floor(diff / 1000);

        // 补零函数
        const pad = (n) => n.toString().padStart(2, '0');

        // return `剩余${days}天 ${pad(hours)}时${pad(minutes)}分${pad(seconds)}秒`;
        return `剩余${days}天 ${pad(hours)}时${pad(minutes)}分`;
    },
    // 获取当前时间 时分秒
    nowDateFun: function (type = '') {
        // 默认年月日时分秒
        let now = new Date();
        let year = now.getFullYear();
        let month = now.getMonth() + 1; // 月份是从0开始的，所以需要+1
        let day = now.getDate();
        let hours = now.getHours();
        let minutes = now.getMinutes();
        let seconds = now.getSeconds();
        const pad = (n) => n.toString().padStart(2, '0');
        let str = `${year}-${pad(month)}-${pad(day)} ${pad(hours)}:${pad(minutes)}`;
        if (type == 'yy-mm-dd') str = `${year}-${pad(month)}-${pad(day)}`;
        return str
    },
    daysUntil: function (fixedDate) {

        const formatDate = (val) => {

            // 处理只有年月日的情况 自动补全时分秒为00:00:00
            let dateStr = /^\d{4}-\d{1,2}-\d{1,2}$/.test(val) ? val + ' 00:00:00' : val
            dateStr = (typeof dateStr === 'string' ? dateStr : dateStr * (dateStr < 1e12 ? 1000 : 1)).replace(/-/g, '/')

            // dateStr = /^\d{4}-\d{1,2}-\d{1,2}$/.test(val)
            // console.log('dateStr',dateStr)
            // console.log('dateStr',dateStr)
            // 分割日期组成部分
            const parts = dateStr.split(' '); //将年月日和时分秒分开
            const partsY = parts[0].split('/');//将年月日 分割
            const partsH = parts[1].split(':');//将年时分秒 分割
            // console.log('partsH',partsH)
            // if(partsH.length<3)partsH
            // // 对月和日进行补零处理
            const padZero = (n) => n.toString().padStart(2, '0');
            const year = partsY[0];
            const month = padZero(partsY[1]);
            const day = padZero(partsY[2]);
            const h = padZero(partsH[0]);
            const m = padZero(partsH[1]);
            const s = padZero(partsH[2]);

            // // 对年月日时分秒进行补零处理
            return `${year}/${month}/${day} ${h}:${m}:${s}`;
        };
        // 修正日期格式

        const formattedDate = formatDate(fixedDate);

        // 将字符串日期转换为日期对象
        const targetDate = new Date(formattedDate);
        // 获取今天的日期
        const today = new Date();
        // 将日期转换为毫秒数
        const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
        // 计算两个日期之间的差异（以毫秒为单位）
        const diff = targetDate - today;
        // 将差异转换为天数
        const daysLeft = Math.ceil(diff / oneDay);
        return diff;

    }
}
